package com.zho.util;

import java.util.Date;

import com.zho.util.Constants.DATE;

public abstract class FilePathUtil {
    
    /** 
    * @param sourceString
    * @param strOldValue
    * @param strNewValue
    * @return new String after replacing the old one with the new one; return
    *         null if any paremeter is null; return empty string if any error;
    *         return original string if no match found
    */
   public static String replaceString(String sourceString, String strOldValue, String strNewValue) {
       try {
           // check for null arguments
           if (sourceString == null || strOldValue == null || strNewValue == null) {
               return sourceString;
           }
           int iBeginIndex;
           int iOldValLen = strOldValue.length();
           String strTemp;
           StringBuffer strReturn = new StringBuffer();

           while ((iBeginIndex = sourceString.indexOf(strOldValue)) != -1) {
               strTemp = sourceString.substring(0, iBeginIndex);
               strReturn.append(strTemp).append(strNewValue);
               sourceString = sourceString.substring(iBeginIndex + iOldValLen);
           }
           strReturn.append(sourceString);
           return strReturn.toString();
       } catch (Exception e) {
           return "";
       }
   }
    
	/**
     * This method will join 2 String.
     * 
     * @param uri1
     * @param uri2
     * @return
     */
    public static String catString(String uri1, String uri2) {

        StringBuffer str = new StringBuffer();
        uri1 = uri1 == null ? "" : uri1.trim();
        uri2 = uri2 == null ? "" : uri2.trim();

        uri1 = replaceString(uri1, "\\", "/");
        uri2 = replaceString(uri2, "\\", "/");

        boolean endSlash1 = uri1.endsWith("/");
        boolean startSlash2 = uri2.startsWith("/");

        if (endSlash1 && startSlash2) {
            uri2 = uri2.substring(1);
        } else if (!(endSlash1 || startSlash2)) {
            uri2 = "/" + uri2;
        }
        str.append(uri1).append(uri2);

        return str.toString();
    }
    /**
     * 
     * @param fileName
     * @return NULL if fileName was NULL or no type.
     */
    public static String getFileType(String fileName) {
        String type = null;
        fileName = (fileName == null ? "" : fileName.trim());
        if (fileName.length() > 0) {
            int i = fileName.lastIndexOf(".");
            if (i > -1) {
                try {
                    type = fileName.substring(i + 1);
                } catch (Exception e) {
                    type = null;
                }

            }
        }
        return type;
    }

    /**
     * 
     * @param fileName
     * @return
     */
    public static String renameFileType(String fileName, String newType) {
        String str;
        fileName = (fileName == null ? "" : fileName.trim());
        newType = (newType == null ? "" : newType.trim());
        if (fileName.length() > 0 && newType.length() > 0) {
            int i = fileName.lastIndexOf(".");
            if (i > -1) {
                try {
                    str = fileName.substring(0, i);
                    if (!newType.startsWith(".")) {
                        newType = "." + newType;
                    }
                    fileName = str + newType;
                } catch (Exception e) {
                }

            }
        }
        return fileName;
    }

    /**
     * 
     * @param fileName
     * @return yyyy-MM-dd/{UUID}.{file_type}
     * @throws Exception
     */
    public static String generateURI(String fileName) throws Exception {
        String folder = DateHelpers.dateToString(new Date(), DATE.DATE_FORMAT_yyyMMdd);
        return catString(folder, RandomHelper.getGUID() + "." + getFileType(fileName));
    }

    public static void main(String[] args) {

    }
}
